<?php
class ControllerModuleLatestRestaurant extends Controller {
	protected function index($setting) {
		$this->language->load('module/latest_restaurant');
		
      	$this->data['heading_title'] = $this->language->get('heading_title');
				
		$this->load->model('restaurant/restaurant');
		
		$this->load->model('tool/image');
		
		$this->data['review_status'] = $this->config->get('config_review_status');
		
		$this->data['restaurants'] = array();
		
		$data = array(
			'sort'  => 's.date_added',
			'order' => 'DESC',
			'start' => 0,
			'limit' => $setting['limit']
		);

		$results = $this->model_restaurant_restaurant->getLatestRestaurants($setting['limit']);
		if(isset($results)) {
			foreach ($results as $result) {
				$image = $result['image'];
				
				if(!$image || !file_exists(DIR_IMAGE . $image)) {
					$image = $result['config']->get('config_logo');
				}
	
				if ($image && file_exists(DIR_IMAGE . $image)) {
					$image = $this->model_tool_image->resize($image, $setting['image_width'], $setting['image_height']);
				} else {
					$image = $this->model_tool_image->resize('no_image.jpg', $setting['image_width'], $setting['image_height']);
				}
				
				if ($this->config->get('config_review_status')) {
					$rating = $result['rating'];
				} else {
					$rating = false;
				}
				
				$this->data['restaurants'][] = array(
					'restaurant_id' => $result['restaurant_id'],
					'thumb'   	 => $image,
					'name'    	 => $result['name'],
					'rating'     => $rating,
					'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
					'href'    	 => $this->url->link('restaurant/restaurant', 'restaurant_id=' . $result['restaurant_id']),
				);
			}
		}

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/latest_restaurant.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/latest_restaurant.tpl';
		} else {
			$this->template = 'default/template/module/latest_restaurant.tpl';
		}

		$this->render();
	}
}
?>